import { createRouter, createWebHistory } from 'vue-router'

// 公共页面
const Login = () => import('../views/common/Login.vue')
const Layout = () => import('../views/common/Layout.vue')

// 司机页面
const DriverDashboard = () => import('../views/driver/Dashboard.vue')
const DriverOrders = () => import('../views/driver/OrderList.vue')
const DriverQualification = () => import('../views/driver/Qualification.vue')
const DriverAccount = () => import('../views/driver/Dashboard.vue') // 暂用Dashboard页面替代

// 管理员页面
const StaffDashboard = () => import('../views/staff/Dashboard.vue')
const StaffMonitor = () => import('../views/staff/Monitor.vue')
const StaffAlerts = () => import('../views/staff/Alerts.vue')
const QualificationsReview = () => import('../views/staff/QualificationsReview.vue') // 使用新创建的页面
const UsersManagement = () => import('../views/staff/System.vue') // 暂用System页面替代
const UserManagement = () => import('../views/staff/UserManagement.vue') // 新增用户管理页面
const StudentManagement = () => import('../views/staff/StudentManagement.vue') // 新增学生管理页面

// 乘客页面 (占位，需要根据实际情况创建)
const PassengerDashboard = () => import('../views/staff/Dashboard.vue') // 暂用员工页面
const PassengerBooking = () => import('../views/staff/Monitor.vue') // 暂用员工页面
const PassengerOrders = () => import('../views/staff/Orders.vue') // 暂用员工页面
const PassengerCurrentTrip = () => import('../views/staff/Alerts.vue') // 暂用员工页面
const PassengerProfile = () => import('../views/staff/Drivers.vue') // 暂用员工页面

// 路由配置
const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  // 统一的主布局路由
  {
    path: '/',
    component: Layout,
    children: [
      // 司机 - 账户管理
      {
        path: 'account',
        name: 'DriverAccount',
        component: () => import('@/views/driver/Account.vue'),
        meta: { title: '账户管理', icon: 'wallet' }
      },
      
      // 司机订单
      {
        path: 'orders',
        name: 'DriverOrders',
        component: DriverOrders,
        meta: { 
          title: '历史订单', 
          icon: 'Tickets',
          requiresDriver: true
        }
      },
      
      // 乘客订单
      {
        path: 'passenger-orders',
        name: 'PassengerOrders',
        component: PassengerOrders,
        meta: { 
          title: '历史订单', 
          icon: 'Tickets',
          requiresPassenger: true
        }
      },
      
      // 司机 - 资质管理
      {
        path: 'qualification',
        name: 'DriverQualification',
        component: DriverQualification,
        meta: { 
          title: '资质管理', 
          icon: 'Document',
          requiresDriver: true
        }
      },
      
      // 管理员 - 行程监控
      {
        path: 'monitor',
        name: 'StaffMonitor',
        component: StaffMonitor,
        meta: { 
          title: '行程监控', 
          icon: 'Monitor',
          requiresAdmin: true
        }
      },
      
      // 管理员 - 求救处理
      {
        path: 'alerts',
        name: 'StaffAlerts',
        component: StaffAlerts,
        meta: { 
          title: '求救处理', 
          icon: 'AlarmClock',
          requiresAdmin: true
        }
      },
      
      // 管理员 - 资质审核
      {
        path: 'qualifications-review',
        name: 'QualificationsReview',
        component: QualificationsReview,
        meta: { 
          title: '资质审核', 
          icon: 'Document',
          requiresAdmin: true
        }
      },
      // 管理员 - 用户管理页面（仅adminRoot账号可见）
      {
        path: 'user-management',
        name: 'UserManagement',
        component: UserManagement,
        meta: { 
          title: '用户管理', 
          icon: 'User',
          requiresAdmin: true,
          requiresAdminRoot: true
        }
      },
      
      // 管理员 - 学生管理页面（仅adminRoot账号可见）
      {
        path: 'student-management',
        name: 'StudentManagement',
        component: StudentManagement,
        meta: { 
          title: '学生管理', 
          icon: 'School',
          requiresAdmin: true,
          requiresAdminRoot: true
        }
      },
      

      
      // 乘客特有页面
      {
        path: 'booking',
        name: 'PassengerBooking',
        component: PassengerBooking,
        meta: { 
          title: '预约拼车', 
          icon: 'Plus',
          requiresPassenger: true
        }
      },
      {
        path: 'current-trip',
        name: 'PassengerCurrentTrip',
        component: PassengerCurrentTrip,
        meta: { 
          title: '当前行程', 
          icon: 'Location',
          requiresPassenger: true
        }
      },
      {
        path: 'profile',
        name: 'PassengerProfile',
        component: PassengerProfile,
        meta: { 
          title: '个人中心', 
          icon: 'UserFilled',
          requiresPassenger: true
        }
      },
      
      // 在路由配置中添加以下路由
      {
        path: '/driver/orders',
        name: 'OrderList',
        component: () => import('@/views/driver/OrderList.vue'),
        meta: { title: '订单列表', requiresAuth: true }
      },
      {
        path: '/orders/:id',
        name: 'OrderDetail',
        component: () => import('@/views/driver/OrderDetail.vue'),
        meta: { title: '订单详情', requiresAuth: true }
      }
    ]
  },
  // 404页面
  {
    path: '/:pathMatch(.*)*',
    redirect: '/login'
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

// 导航守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  const userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}')
  
  // 如果要访问的不是登录页，且没有token，则重定向到登录页
  if (to.path !== '/login' && !token) {
    next('/login')
    return
  }
  
  // 如果已登录但访问登录页，则根据用户身份重定向
  if (to.path === '/login' && token) {
    if (userInfo.isDriver) {
      next('/account')
    } else if (userInfo.isAdmin) {
      next('/monitor')
    } else {
      next('/booking')
    }
    return
  }
  
  // 检查权限
  if (to.meta.requiresAdmin && !userInfo.isAdmin) {
    if (userInfo.isDriver) {
      next('/account')
    } else {
      next('/booking')
    }
    return
  }
  
  if (to.meta.requiresDriver && !userInfo.isDriver) {
    if (userInfo.isAdmin) {
      next('/monitor')
    } else {
      next('/booking')
    }
    return
  }
  
  // 检查是否需要admin账号权限
  if (to.meta.requiresAdminAccount && userInfo.phone !== 'admin') {
    next('/monitor')
    return
  }
  
  // 检查是否需要adminRoot账号权限
  if (to.meta.requiresAdminRoot && userInfo.phone !== 'adminRoot') {
    next('/monitor')
    return
  }
  
  // 只有当用户既不是管理员也不是司机时，才检查乘客权限
  if (to.meta.requiresPassenger && (userInfo.isAdmin || userInfo.isDriver)) {
    if (userInfo.isAdmin) {
      next('/monitor')
    } else {
      next('/account')
    }
    return
  }
  
  next()
})

export default router
